//
//  JinXingZhongViewController.m
//  XiangMu
//
//  Created by 康世标 on 15/8/12.
//  Copyright (c) 2015年 康世标. All rights reserved.
//

#import "JinXingZhongViewController.h"
#import "IQKeyboardManager.h"
@interface JinXingZhongViewController ()

@end

@implementation JinXingZhongViewController

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:YES];
    
    [self mnporeg];
    [self.datasDic removeAllObjects];
    [self getHuoQuDingDan];

}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.view.backgroundColor=[UIColor redColor];
    [self.tableView1 registerNib:[UINib nibWithNibName:@"WoDeDingDanTableViewCell" bundle:nil] forCellReuseIdentifier:@"coll1"];
    self.tableView1.backgroundColor = CL_efeff4;
    self.tableView1.separatorColor = CL_efeff4;
    _inderowTag=-1;
      [IQKeyboardManager  sharedManager];
     page =1;
    _selectedArr =[[NSMutableArray alloc]initWithCapacity:0];
     self.datasDic = [[NSMutableArray alloc]initWithCapacity:0];
     self.dic = [[NSMutableDictionary alloc]initWithCapacity:0];
    
   
    // 下拉刷新
    [self.tableView1 addPullToRefreshWithActionHandler:^
     {
         
         [self performSelector:@selector(xialaSuaXin) withObject:self afterDelay:1.5];
         
     }];
    //上推加载更多
    [self.tableView1 addInfiniteScrollingWithActionHandler:^{
        
        [self performSelector:@selector(JiaZaiGengDuo) withObject:self afterDelay:1];
    }];

}
- (void)xialaSuaXin{
    page =1;
    [self.datasDic removeAllObjects];
    [self getHuoQuDingDan];
    
}

- (void)JiaZaiGengDuo{
    
    page ++;
    if (page<=self.indePage)
    {
        
        [self getHuoQuDingDan];
    }
    else{
        [self.tableView1.infiniteScrollingView stopAnimating];
        
        [self.tableView1.pullToRefreshView stopAnimating];
        
        hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
        
        hud.mode=MBProgressHUDModeText;
        
        hud.labelText=@"没有更多数据了";
        
        hud.removeFromSuperViewOnHide=YES;
        
        [hud hide:YES afterDelay:1.5];
        
    }
}

- (void)mnporeg{
    
    hud =[MBProgressHUD showHUDAddedTo:self.view animated:YES];
    hud.labelText = @"数据加载中...";
}
- (void)getHuoQuDingDan{
   
    long  userId =[[[NSUserDefaults standardUserDefaults]objectForKey:@"ID"] longValue];
    [self.dic setValue:@(userId) forKey:@"userId"];
    [self.dic setValue:@(page) forKey:@"page"];
    [self.dic setValue:@(10) forKey:@"rows"];
    [self.dic setValue:URL_JINXINGZHONG forKey:@"url"];
    
    [QingQiuShuJu HuoQuWoDeDingDan:self.dic Block:^(NSDictionary *dic, BOOL isOK) {
        
      
        if (isOK) {
            
            [self.datasDic addObjectsFromArray:[dic valueForKey:@"data"]];
            [hud hide:YES];
            [self.tableView1 reloadData];
            self.indePage = [[dic valueForKey:@"totalPage"] intValue];
            [self.tableView1.pullToRefreshView stopAnimating];
            [self.tableView1.infiniteScrollingView stopAnimating];
            
        }
        else{
            hud.mode = MBProgressHUDModeText;
            hud.labelText = @"网络加载失败";
            [hud hide:YES afterDelay:2];
    
            [self.tableView1.pullToRefreshView stopAnimating];
            [self.tableView1.infiniteScrollingView stopAnimating];
        }
    }];
    
}

#pragma mark---UITableViewDataSource,
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)sectio{
    
    return self.datasDic.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    WoDeDingDanTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"coll1" forIndexPath:indexPath];
    cell.xuDanBtn.hidden = YES;
    cell.jieShuBtn.hidden = YES;
//    [cell.jieShuBtn addTarget:self action:@selector(jieShuBtn1:) forControlEvents:UIControlEventTouchUpInside];
//    [cell.quXiaoBtn addTarget:self action:@selector(quXiaoBtn1:) forControlEvents:UIControlEventTouchUpInside ];
//    [cell.quDingBtn addTarget:self action:@selector(queDing:) forControlEvents:UIControlEventTouchUpInside];
//    [cell.xuDanBtn addTarget:self action:@selector(xuDingDan:) forControlEvents:UIControlEventTouchUpInside];
//    cell.xuDanBtn.tag = indexPath.row;
    
    
    NSDictionary * dic1 = [self.datasDic objectAtIndex:indexPath.row];
    
    [cell.imagView1 sd_setImageWithURL:[NSURL URLWithString:[dic1 valueForKey:@"headImg"]==[NSNull null]?@"":[dic1 valueForKey:@"headImg"]] placeholderImage:[UIImage imageNamed:@"like"]];
    
    
    cell.yiYuanLable1.text = [dic1 valueForKey:@"cusName"]==[NSNull null]?@"":[dic1 valueForKey:@"cusName"];
    cell.shiJianLable.text = [dic1 valueForKey:@"timeType"]==[NSNull null]?@"":[dic1 valueForKey:@"timeType"];
    
    cell.riQiLabel.text = [self upDated:[dic1 valueForKey:@"startTime"]==[NSNull null]?0:[[dic1 valueForKey:@"startTime"] longValue]];
    
    cell.DiZhiLable.text =[dic1 valueForKey:@"cusAddress"]==[NSNull null]?@"":[dic1 valueForKey:@"cusAddress"];
    cell.jiaGeLabel.text =[NSString stringWithFormat:@"%@",[dic1 valueForKey:@"price"]==[NSNull null]?@"":[dic1 valueForKey:@"price"] ];

    
//   
//    cell.quDingBtn.tag = indexPath.row;
//    
//    cell.textField1.tag=indexPath.row;
//    cell.jieShuBtn.tag=indexPath.row;
//    
//    cell.textField1.hidden=YES;
//    cell.quXiaoBtn.hidden=YES;
//    cell.quDingBtn.hidden=YES;
//    for (NSNumber *number in _selectedArr) {
//        
//        if (indexPath.row ==[number integerValue])
//        {
//            cell.textField1.hidden=NO;
//            cell.quXiaoBtn.hidden=NO;
//            cell.quDingBtn.hidden=NO;
//
//        }
//    }
    return cell;
    
}
- (void )queDing:(UIButton *)sender{
    
     hud =[MBProgressHUD showHUDAddedTo:self.view animated:YES];
    
    NSDictionary * dic1 = [self.datasDic objectAtIndex:sender.tag];
    
    WoDeDingDanTableViewCell * cell = (WoDeDingDanTableViewCell *)[self.tableView1 cellForRowAtIndexPath:[NSIndexPath indexPathForRow:sender.tag inSection:0]];
    
    
    NSString * token =[[NSUserDefaults standardUserDefaults]objectForKey:@"msg"];
    
    [QingQiuShuJu tiQianJieShu:@{@"orderId":[dic1 valueForKey:@"id"],@"alipayNum":cell.textField1.text,@"ticket":token} block:^(NSDictionary *dic, BOOL isOK)
    {
        
        if (isOK) {
            
     
            
            if ([[dic valueForKey:@"status"] intValue]==200) {
                
                hud.customView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"37x-Checkmark.png"]];
                hud.mode = MBProgressHUDModeCustomView;
                hud.labelText =  [dic valueForKey:@"msg"];
                [hud hide:YES afterDelay:2];
                [self getHuoQuDingDan];
            }
            else{
                hud.mode = MBProgressHUDModeText;
                
                hud.labelText =  [dic valueForKey:@"msg"];
                [hud hide:YES afterDelay:2];

            }
        }
        else{
            
        }
    }];
    
}
#pragma mark---,UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
//    _wideCell=0;
//    for (NSNumber *numbe in _selectedArr) {
//        
//        
//        if (indexPath.row==[numbe integerValue]) {
//            
//            _wideCell=80;
//        }
//        
//    }
    
//    return 171+_wideCell;
    
    return 128;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    XiangXiViewController *x =[[XiangXiViewController alloc]init];
      NSDictionary * dic1 = [self.datasDic objectAtIndex:indexPath.row];
    
     x.dic = dic1;
    
    
    [self.navigationController pushViewController:x animated:YES];
    

}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
//-(void)jieShuBtn1:(UIButton *)sender{
//    
//    WoDeDingDanTableViewCell*cell1=(WoDeDingDanTableViewCell *)[_tableView1 cellForRowAtIndexPath:[NSIndexPath indexPathForRow:sender.tag inSection:0]];
//    _inderowTag=sender.tag;
//    
//    
//    
//    BOOL isSelected =NO;
//    
//    for (NSNumber *numner in _selectedArr) {
//        
//        NSInteger i=[numner integerValue];
//        
//        if (i==sender.tag) {
//            
//            isSelected=YES;
//        }
//    }
//    
//    if (isSelected) {
//        
//        NSNumber *number=[NSNumber numberWithInteger:sender.tag];
//        
//        [_selectedArr removeObject:number];
//        
//        cell1.textField1.hidden=YES;
//        cell1.quDingBtn.hidden=YES;
//        cell1.quXiaoBtn.hidden=YES;
//        
//    }
//    else{
//        
//        NSNumber *number=[NSNumber numberWithInteger:sender.tag];
//        
//        [_selectedArr addObject:number];
//        
//        cell1.textField1.hidden=NO;
//        cell1.quXiaoBtn.hidden=NO;
//        cell1.quDingBtn.hidden=NO;
//    }
//
//    _wideCell=80;
//    [self.tableView1 reloadData];
//    
//}
//- (void)quXiaoBtn1:(UIButton *)sender{
//    _wideCell=0;
//    [_selectedArr removeAllObjects];
//    [self.tableView1 reloadData];
//    
//}
//#pragma mark --续约订单
//- (void)xuDingDan:(UIButton *)sender{
//    
//    XiaDanViewController * x =[[XiaDanViewController alloc]init];
//    x.dic = [self.datasDic objectAtIndex:sender.tag];
//    
////    [self.navigationController pushViewController:x animated:YES];
//    
//
//}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    [self.tableView1 endEditing:YES];
}
-(NSString *)upDated:(long )str{
    
    if (str==0) {
        
        return @"";
        
    }
    
    NSDate * date = [NSDate dateWithTimeIntervalSince1970:str/1000];
    
    NSDateFormatter * dataFormatter = [[NSDateFormatter alloc]init];
    
    [dataFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    
    NSString * string= [dataFormatter stringFromDate:date];
    
    return string;
    
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
